<extend name="./public/frame.html"/>
<block name="content">
    <style>
        .layui-form-label{width: 12%;}
        .layui-input-block{margin-left: 12%;}
    </style>
    <form class="layui-form layui-form-pane1" id="testForm" method="post" enctype="multipart/form-data">
        <table class="layui-table">
            <tr>
                <td style="text-align: center;width: 15%">优惠券名称</td>
                <td><input type="text" name="coupon_name" id="coupon_name" lay-verify="required" placeholder="优惠券名称" class="layui-input" style="width: 500px;"></td>
            </tr>

            <tr>
                <td style="text-align: center;width: 15%">领取方式</td>
                <td>
                    <div class="layui-input-inline" style="width: 200px;">
                        <select name="is_recommed" lay-filter="is_recommed" id="is_recommed">
                            <?php foreach($isRecommedName as $key=>$value):?>
                            <option value="<?php echo $key;?>"><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 15%">是否最大优惠券</td>
                <td>
                    <div class="layui-input-inline" style="width: 200px;">
                        <select name="max_welfare" lay-filter="max_welfare" id="max_welfare">
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr class="newUser" style="display: none;">
                <td style="text-align: center;width: 15%">只为新用户</td>
                <td>
                    <input type="checkbox" name="is_newuser" id="is_newuser" value="0" lay-skin="switch" lay-filter="switchCheck" lay-text="ON|OFF" >
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 15%">优惠券类型</td>
                <td>
                    <div class="layui-input-inline" style="width: 200px;">
                        <select name="money_type" lay-filter="money_type" id="money_type">
                            <?php foreach($moneyTypeName as $key=>$value):?>
                            <option value="<?php echo $key;?>" id="money_type<?php echo $key;?>"><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;">优惠形式</td>
                <td>
                    <span style="display: block;float: left;padding-top: 7px;margin-right: 10px;">
                        满
                    </span>
                    <input type="text" name="over_money" id="over_money" lay-verify="required" placeholder="0元（无门槛）" class="layui-input" style="width: 120px;float: left;margin-right: 10px;" onkeyup="num(this)">
                    <span style="display: block;float: left;padding-top: 7px;margin-right: 10px;" class="words_01">
                        减
                    </span>
                    <input type="text" name="money" id="money" lay-verify="required" placeholder="优惠" class="layui-input" style="width: 120px;float: left;margin-right: 10px;" onkeyup="num(this)">
                    <span style="display: block;float: left;padding-top: 7px;" class="words_02">
                        元
                    </span>
                </td>
            </tr>

            <tr>
                <td style="text-align: center;">使用期限</td>
                <td>
                    <table class="layui-table">
                        <tr>
                            <td style="text-align: center;width: 10%">类型</td>
                            <td>
                                <input type="radio" name="type_day" lay-filter="type_day" value="1" title="固定日期" checked>
                                <input type="radio" name="type_day" lay-filter="type_day" value="2" title="非固定日期" >
                            </td>
                        </tr>
                        <tr class="fixed_time">
                            <td style="text-align: center;">日期</td>
                            <td>
                                <input type="text" name="valid_start_time"  class="layui-input" id="valid_start_time" placeholder="开始期限" style="width: 120px;float: left;margin-right: 10px;">
                                <span style="display: block;float: left;padding-top: 7px;margin-right: 10px;">-</span>
                                <input type="text" name="valid_end_time" class="layui-input" id="valid_end_time" placeholder="结束期限" style="width: 120px;float: left;">
                            </td>
                        </tr>
                        <tr class="no_fixed_time" style="display: none;">
                            <td style="text-align: center;">日期</td>
                            <td>
                                <span style="display: block;float: left;padding-top: 7px;margin-right: 10px;">自用户领取之日起</span>
                                <input type="text" name="start_day"   class="layui-input" id="start_day" placeholder="day" style="width: 120px;float: left;margin-right: 10px;">
                                <span style="display: block;float: left;padding-top: 7px;margin-right: 10px;">天后生效,有效期</span>
                                <input type="text" name="duration_day" class="layui-input" id="duration_day" placeholder="day" style="width: 120px;float: left;">
                                <span style="display: block;float: left;padding-top: 7px;">天</span>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <?php if($orderType == 3):?>
            <tr>
                <td style="text-align: center;">电子围栏</td>
                <td>
                    <input type="checkbox" data-type="allDianzi" title="全部" lay-verify="required|region">
                    <?php foreach($interRailList as $key=>$value):?>
                    <input type="checkbox" name="inter_rail[]" title="{$value.rail_name}" value="{$value.id}" class="inter_rail">
                    <?php endforeach;?>
                </td>
            </tr>
            <?php endif;?>
            <tr>
                <td style="text-align: center;width: 15%">限领(<span style="color: green;">张</span>)</td>
                <td><input type="text" name="limit_num" id="limit_num" lay-verify="required" placeholder="限领张数" value="1" class="layui-input" style="width: 500px;"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 15%">发行总数</td>
                <td><input type="text" name="total_num" id="total_num" placeholder="发行总数（默认空或0不限）" value="" class="layui-input" style="width: 500px;"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 15%">发放方式</td>
                <td>
                    <div class="layui-input-inline" style="width: 200px;">
                        <select name="receive_type" lay-filter="receive_type" id="receive_type">
                            <?php foreach($receiveTypeName as $key=>$value):?>
                            <option value="<?php echo $key;?>"><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="padding-left: 100px;">
                    <input type="hidden" value="<?php echo $admin_activity_id;?>" name="admin_activity_id">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form','laydate'], function () {
            form = layui.form;
            var laydate = layui.laydate;
            laydate.render({
                elem: '#valid_start_time'
                ,min: '<?php echo date("Y-m-d");?>'
            });
            laydate.render({
                elem: '#valid_end_time'
                ,min: '<?php echo date("Y-m-d");?>'
            });
            //优惠券类型选择
            form.on('select(money_type)',function (data) {
                if(data.value == 1){
                    $(".words_01").html('减');
                    $(".words_02").html('元');
                }else{
                    $(".words_01").html('打');
                    $(".words_02").html('折');
                }
            })
            //有效期限选择
            form.on('radio(type_day)', function(data){
                if(data.value == 1){
                    $(".fixed_time").css('display','');
                    $(".no_fixed_time").css('display','none');
                }else if(data.value == 2){
                    $(".fixed_time").css('display','none');
                    $(".no_fixed_time").css('display','');
                }
            });
            //领取方式
            form.on('select(is_recommed)',function(data){

                if(data.value == 2 || data.value == 1){
                    $(".newUser").css('display','none');
                }else{
                    $(".newUser").css('display','');
                }
            })
            form.on('checkbox', function(data){
                var type = $(this).data('type');
                if(type == 'allDianzi'){
                    var child = $('.inter_rail');
                    child.each(function(index, item){
                        item.checked = data.elem.checked;
                    });
                    form.render('checkbox');
                }
            });
            //只为新用户操作
            form.on('switch(switchCheck)', function(data){
                if(!this.checked){
                    var value = 0;
                }else{
                    var value = 1;
                }
                $("#is_newuser").val(value);
            });
            form.on('submit(*)', function(data){
                var type_day = $("input[name='type_day']:checked").val();
                if(type_day == 1){
                    var valid_start_time = $("#valid_start_time").val();
                    var valid_end_time = $("#valid_end_time").val();
                    if(valid_start_time == '' || valid_end_time == ''){
                        layer.msg('优惠券使用期限不能为空',{icon: 5});return false
                    }
                }else{
                    var start_day = $("#start_day").val();
                    var duration_day = $("#duration_day").val();
                    if(start_day == '' || duration_day == ''){
                        layer.msg('优惠券使用期限不能为空',{icon: 5});return false
                    }
                }
                $.ajax({
                    url:'{:url("/admin/AdminActivity/addCoupon")}',
                    async:true,
                    dataType:'json',
                    type:'POST',
                    data: $(data.form).serialize(),
                    success:function(res){
                        if(res.code==1){
                            layer.alert(res.msg, {icon: 6},function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload()
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                return false;
            });
        });

        //验证只能输入数字
        function num(object) {
            var value   = object.value;
            var id      = object.id;
            if (isNaN(value)){
                layer.msg('请输入正确的数字',{icon: 5});
                $("#"+id).val('');
                return false;
            }
        }
    </script>
</block>
